package com.yycx.app.provider.service.impl;

import com.yycx.app.provider.service.AppCommonService;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import com.yycx.module.user.client.entity.AppUser;
import com.yycx.module.user.provider.service.AppUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zyf
 */
@Slf4j
@Service
public class AppCommonImplServiceImpl implements AppCommonService {

    @Resource
    private AppUserService appUserService;
    @Resource
    private SysCompanyService companyService;

    @Override
    public ResultBody changeBusNUm(Long userId, Integer busNum) {
        ApiAssert.isNotEmpty("数量不能为空", busNum);
        AppUser appUser = appUserService.getById(userId);
        if (FlymeUtils.isNotEmpty(appUser)) {
            Long companyId = appUser.getCompanyId();
            if (FlymeUtils.isNotEmpty(companyId)) {
                SysCompany sysCompany = companyService.getById(companyId);
                Integer busTotalNum = FlymeUtils.getInteger(sysCompany.getBusTotalNum(), 0);
                if (busNum > busTotalNum) {
                    return ResultBody.failed("总数量不足");
                }
                companyService.updateBusNum(companyId, -busNum);
            }
        }
        return ResultBody.ok();
    }

}
